package com.haojiang.mh.ui.activity

import android.view.View
import com.haojiang.mh.R
import com.haojiang.mh.cache.Const
import com.haojiang.mh.service.ble.BleHandle
import com.haojiang.mh.service.event.OneEvent
import com.haojiang.mh.ui.base.BaseActivity
import com.haojiang.mh.utils.ActManager
import kotlinx.android.synthetic.main.activity_common_title.*
import kotlinx.android.synthetic.main.activity_search_device_fail.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 连接设备失败
 *
 * Created by ZapFive on 2019-05-26.
 *
 * wuzhuang@mirahome.me
 */
class SearchDeviceFailActivity : BaseActivity() {

    private var entrance = -1 //1:蓝牙未打开 2:搜索不到设备

    override fun getLayoutId() = R.layout.activity_search_device_fail

    override fun getTitleName() = ""

    override fun initActivity() {
        entrance = intent.getIntExtra(Const.EXTRA_DATA, -1)
    }

    override fun initView() {
        tv_activity_right.visibility = View.VISIBLE
        tv_activity_right.text = getString(R.string.txt_cancel_add)
        tv_activity_right.setOnClickListener {
            if (BleHandle.instance.isConnected()) {
                BleHandle.instance.disConnectDevice()
                BleHandle.instance.onDeviceDisconnect(true)
            } else {
                ActManager.redirectToBottomActivity()
                context.finish()
            }
        }
        bt_retry.setOnClickListener {
            if (entrance == 1 && !BleHandle.instance.isBleEnable()) {
                showToast(R.string.dialog_open_bluetooth_tip)
                return@setOnClickListener
            }
            redirectNoParamAndFinish(DeviceListActivity::class.java)
        }
        tv_tip_3.setOnClickListener {
            //TODO 跳转蓝牙打开提示
        }
        when (entrance) {
            1 -> {
                aiv_show_img.setImageResource(R.drawable.img_no_bluetooth)
                layout_no_device.visibility = View.GONE
                layout_no_bluetooth.visibility = View.VISIBLE
            }
            2 -> {
                aiv_show_img.setImageResource(R.drawable.img_no_device)
                layout_no_device.visibility = View.VISIBLE
                layout_no_bluetooth.visibility = View.GONE
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onReceiveEvent(event: OneEvent) {
        if (event.type != Const.DISCONNECT_DEVICE) return
        ActManager.redirectToBottomActivity()
        context.finish()
    }
}